<template>
  <div class="article-list-container">
    <div v-if="loading" class="loading-state">
      <div class="spinner"></div>
      <p>正在加载文章...</p>
    </div>
    <div v-else-if="articles.length === 0" class="empty-state">
      <i class="ri-archive-line"></i>
      <p>该分类下暂无文章</p>
    </div>
    <div v-else class="article-list">
      <router-link
        v-for="item in articles"
        :key="item.id"
        :to="`/article/${item.id}`"
        class="article-card-link"
      >
        <article v-if="item.coverType === 'large'" class="article-card large-image">
          <div class="cover-wrapper" :style="{ backgroundImage: `url(${item.cover})` }"></div>
          <span class="category-tag">{{ item.category }}</span>
          <div class="content-wrapper">
            <h2 class="title">{{ item.title }}</h2>
            <p class="description">{{ item.description }}</p>
            <div class="meta">
              <div class="meta-left">
                <span class="author"> <i class="ri-user-line"></i> {{ item.author }} </span>
                <span class="views"> <i class="ri-eye-line"></i> {{ item.views }} </span>
                <span class="likes"> <i class="ri-thumb-up-line"></i> {{ item.likes }} </span>
                <span class="comments"> <i class="ri-chat-3-line"></i> {{ item.comments }} </span>
              </div>
              <span class="date">
                <i class="ri-calendar-2-line"></i> {{ timeAgo(item.date) }}
              </span>
            </div>
          </div>
        </article>

        <article
          v-else
          class="article-card"
          :class="{
            'small-image': item.coverType === 'small',
            'no-image': item.coverType === 'none',
          }"
        >
          <div v-if="item.coverType === 'small'" class="cover-wrapper">
            <img :src="item.cover" :alt="item.title" class="cover-image" />
            <span class="category-tag mobile-category" v-if="item.category">{{
              item.category
            }}</span>
          </div>
          <div class="content-wrapper">
            <h2 class="title">{{ item.title }}</h2>
            <p v-if="item.coverType === 'none' || item.coverType === 'small'" class="description">
              {{ item.description }}
            </p>
            <div class="meta pc-meta">
              <div class="meta-left">
                <span class="category-tag desktop-category">{{ item.category }}</span>
                <span class="author">
                  <i class="ri-user-line"></i> {{ item.author || 'Admin' }}
                </span>
                <span class="views"> <i class="ri-eye-line"></i> {{ item.views || 0 }} </span>
                <span class="likes"> <i class="ri-thumb-up-line"></i> {{ item.likes || 0 }} </span>
                <span class="comments">
                  <i class="ri-chat-3-line"></i> {{ item.comments || 0 }}
                </span>
              </div>
              <span class="date">
                <i class="ri-calendar-2-line"></i> {{ timeAgo(item.date) }}
              </span>
            </div>
            <div class="meta-bottom mobile-meta">
              <div class="meta-left">
                <span class="author">
                  <i class="ri-user-line"></i> {{ item.author || 'Admin' }}
                </span>
                <span class="views"> <i class="ri-eye-line"></i> {{ item.views || 0 }} </span>
              </div>
              <span class="date">
                <i class="ri-calendar-2-line"></i> {{ timeAgo(item.date) }}
              </span>
            </div>
          </div>
        </article>
      </router-link>
    </div>
  </div>
</template>

<script setup>
import { timeAgo } from '@/utils/timeUtils'
/**
 * @prop {Array} articles - 文章数组，每个文章对象包含 coverType 字段
 * @prop {Boolean} loading - 加载状态
 */
defineProps({
  articles: { type: Array, required: true },
  loading: { type: Boolean, default: false },
})
</script>

<style scoped>
/* Reset and Base */
.article-list {
  display: flex;
  flex-direction: column;
  gap: 1.2em; /* Reduced gap */
}
.article-card-link {
  text-decoration: none;
  color: inherit;
  display: block;
}
.article-card {
  background: var(--bg-base);
  border-radius: var(--radius-medium); /* Softer radius */
  border: 1px solid var(--border-light);
  overflow: hidden;
  display: flex;
  transition:
    transform 0.25s ease-out,
    box-shadow 0.25s ease-out;
}
.article-card-link:hover .article-card {
  /* This rule will now be overridden by more specific ones */
}

/* Empty State */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 4rem 2rem;
  text-align: center;
  background-color: var(--bg-base);
  border-radius: var(--radius-large);
  border: 1px solid var(--border-light);
  min-height: 200px;
  color: var(--text-secondary);
}

.empty-state .ri-archive-line {
  font-size: 4rem;
  margin-bottom: 1rem;
  color: var(--text-placeholder);
}

.empty-state p {
  font-size: 1rem;
  font-weight: 500;
}

/* Image Wrapper */
.cover-wrapper {
  overflow: hidden;
  position: relative;
}
.cover-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}
.article-card-link:hover .cover-image {
  transform: scale(1.05);
}

/* Content Area */
.content-wrapper {
  padding: 1.2em; /* Reduced padding */
  display: flex;
  flex-direction: column;
  flex: 1;
}
.title {
  font-size: 1.1em; /* Reduced font size */
  font-weight: 600;
  color: var(--text-primary);
  margin: 0 0 0.4em;
  line-height: 1.5;
  transition: color 0.2s ease;
}
.article-card-link:hover .title {
  color: var(--color-primary);
}
.description {
  font-size: 0.85em; /* Reduced font size */
  color: var(--text-regular);
  margin: 0 0 auto;
  line-height: 1.6;
  display: -webkit-box;
  -webkit-line-clamp: 1; /* Clamp to 1 line */
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  min-height: 1.6em; /* Maintin space for 1 line */
}
.meta {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 1em; /* Reduced gap */
  font-size: 0.85em; /* Reduced font size */
  color: var(--text-secondary);
  margin-top: 1em; /* Reduced margin */
}
.meta span {
  display: inline-flex;
  align-items: center;
  gap: 0.3em;
}
.category-tag {
  font-size: 0.8em;
  color: var(--color-primary);
  background: transparent;
  padding: 0;
}

/* --- Layout Variations --- */

/* Large Image: Total Redesign */
.article-card.large-image {
  height: 280px;
  position: relative;
  padding: 0;
  border: none; /* No border for a cleaner look */
  background-color: var(--bg-light);
  border-radius: var(--radius-large); /* Consistent radius */
  box-shadow: var(--shadow-light);
  transition:
    transform 0.3s ease,
    box-shadow 0.3s ease;
}
.article-card-link:hover .article-card.large-image {
  transform: translateY(-5px);
  box-shadow: var(--shadow-large);
}

.large-image .cover-wrapper {
  position: absolute;
  inset: 0;
  background-size: cover;
  background-position: center;
  border-radius: var(--radius-large); /* Match parent radius */
  transition: transform 0.4s ease;
}
.article-card-link:hover .large-image .cover-wrapper {
  transform: scale(1.05);
}

.large-image .content-wrapper {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  color: #fff;
  padding: 1.5em;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.8) 0%, transparent 100%);
}
.large-image .title {
  font-size: 1.4em; /* Reduced font size */
  color: #fff;
  text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.5);
}
.article-card-link:hover .large-image .title {
  color: #fff;
}
.large-image .description {
  font-size: 0.9em; /* Added font size */
  color: rgba(255, 255, 255, 0.9);
  -webkit-line-clamp: 2;
}
.large-image .meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: rgba(255, 255, 255, 0.85);
  border-top: 1px solid rgba(255, 255, 255, 0.2);
  padding-top: 1em;
  margin-top: 1em;
}
.large-image .meta-left {
  display: flex;
  align-items: center;
  gap: 1.2em;
}
.large-image > .category-tag, /* Direct child selector */
.large-image .meta .category-tag {
  /* This rule is for the v-else block which we dont touch */
  position: absolute;
  top: 1.5em;
  left: 1.5em;
  z-index: 3;
  color: #fff;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(6px);
  padding: 0.4em 0.9em;
  border-radius: var(--radius-round);
}
/* This is for the v-else block */
.small-image .meta .category-tag,
.no-image .meta .category-tag {
  position: static;
  color: var(--color-primary);
  background: rgba(var(--rgb-primary), 0.1);
  border: none;
  backdrop-filter: none;
}

/* Small Image */
.article-card.small-image {
  flex-direction: row;
  align-items: stretch;
  min-height: 160px;
}
.small-image .cover-wrapper {
  flex: 0 0 220px;
}
.small-image .title {
  font-size: 1.1em;
}
.small-image .description {
  -webkit-line-clamp: 3; /* Allow 3 lines for small cards */
  min-height: 4.8em; /* Maintin space for 3 lines */
}
.small-image .content-wrapper {
  padding: 1.2em 1.5em;
  justify-content: center;
}

/* No Image */
.article-card.no-image .content-wrapper {
  padding: 1em; /* Reduced padding */
}

/* --- Responsive --- */
@media (max-width: 768px) {
  .article-card.small-image {
    flex-direction: column;
  }
  .small-image .cover-wrapper {
    height: 220px;
  }
}

@media (max-width: 576px) {
  .article-card.small-image {
    flex-direction: row;
    align-items: stretch;
    min-height: 130px;
    height: 130px;
    box-sizing: border-box;
  }
  .small-image .cover-wrapper {
    flex: 0 0 40%;
    width: 40%;
    height: 100%;
    max-height: 130px;
    min-width: 80px;
    border-radius: var(--radius-medium) 0 0 var(--radius-medium);
    overflow: hidden;
    display: flex;
    align-items: stretch;
    background: #f5f5f5;
    position: relative;
  }
  .small-image .cover-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
  }
  .small-image .category-tag.mobile-category {
    display: inline-block;
    position: absolute;
    top: 8px;
    left: 8px;
    background: rgba(0, 0, 0, 0.55);
    color: #fff;
    font-size: 0.75em;
    padding: 0.18em 0.8em;
    border-radius: 1em;
    z-index: 2;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    font-weight: 500;
    letter-spacing: 0.02em;
    backdrop-filter: blur(2px);
  }
  .small-image .category-tag.desktop-category,
  .pc-meta {
    display: none !important;
  }
  .mobile-meta {
    display: flex !important;
  }
  .small-image .content-wrapper {
    flex: 1 1 60%;
    width: 60%;
    padding: 0.7em 1em;
    display: flex;
    flex-direction: column;
    justify-content: center;
    min-width: 0;
  }
  .small-image .title {
    font-size: 1em;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0 0 0.3em;
    line-height: 1.5;
    white-space: normal;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    word-break: break-all;
  }
  .small-image .description {
    display: none !important;
  }
  .small-image .meta {
    font-size: 0.8em;
    gap: 0.7em;
    margin-top: 0.2em;
    flex-wrap: wrap;
    min-height: 1.2em;
    align-items: flex-start;
  }
  .small-image .meta-bottom {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: auto;
    font-size: 0.8em;
    color: var(--text-secondary);
    gap: 0.5em;
    width: 100%;
  }
  .small-image .meta-bottom .meta-left {
    display: flex;
    align-items: center;
    gap: 0.7em;
  }
  .small-image .meta-bottom .author,
  .small-image .meta-bottom .views {
    color: var(--text-secondary);
    font-size: 0.95em;
    background: none;
    border-radius: 1em;
    padding: 0;
  }
  .small-image .meta-bottom .date {
    margin-left: auto;
    color: var(--text-secondary);
    font-size: 0.85em;
  }
  .small-image .meta .likes,
  .small-image .meta .comments {
    display: none;
  }

  /* Responsive adjustments for large image cards on mobile */
  .large-image .description {
    -webkit-line-clamp: 1;
  }

  .article-card.large-image {
    height: 160px;
    min-height: unset;
  }
  .large-image .content-wrapper {
    padding: 1em 0.9em 0.9em 0.9em;
  }
  .large-image .title {
    font-size: 1em;
    margin-bottom: 0.3em;
  }
  .large-image .description {
    font-size: 0.8em;
    -webkit-line-clamp: 1;
    min-height: 1.2em;
    margin-bottom: 0.5em;
  }
  .large-image .meta {
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;
    padding-top: 0.5em;
    margin-top: 0.5em;
    font-size: 0.85em;
    border-top: none;
    gap: 1em;
  }
  .large-image .meta-left {
    display: flex;
    align-items: center;
    gap: 0.7em;
  }
  .large-image .meta-left .author,
  .large-image .meta-left .views {
    display: inline-flex;
    align-items: center;
    gap: 0.2em;
    color: #fff;
    font-size: 0.95em;
    background: rgba(0, 0, 0, 0.15);
    border-radius: 1em;
    padding: 0.1em 0.6em;
  }
  .large-image .meta-left .likes,
  .large-image .meta-left .comments {
    display: none;
  }
  .large-image .date {
    margin-left: auto;
    color: rgba(255, 255, 255, 0.7);
    font-size: 0.85em;
  }
  .large-image > .category-tag {
    top: 0.7em;
    left: 0.7em;
    padding: 0.25em 0.7em;
    font-size: 0.75em;
  }
  .no-image .mobile-meta {
    display: flex !important;
    align-items: center;
    justify-content: space-between;
    margin-top: 0.2em;
    margin-bottom: 0.1em;
    font-size: 0.8em;
    color: var(--text-secondary);
    gap: 0.5em;
    width: 100%;
  }
  .no-image .mobile-meta .meta-left {
    display: flex;
    align-items: center;
    gap: 0.7em;
  }
  .no-image .mobile-meta .author,
  .no-image .mobile-meta .views {
    color: var(--text-secondary);
    font-size: 0.95em;
    background: none;
    border-radius: 1em;
    padding: 0;
  }
  .no-image .mobile-meta .date {
    margin-left: auto;
    color: var(--text-secondary);
    font-size: 0.85em;
    white-space: nowrap;
  }
  .no-image .description {
    margin-bottom: 0.5em;
  }
}
@media (min-width: 577px) {
  .small-image .category-tag.mobile-category,
  .mobile-meta {
    display: none !important;
  }
  .pc-meta {
    display: flex !important;
    align-items: center;
    justify-content: space-between;
    gap: 1em;
    width: 100%;
  }
  .pc-meta .meta-left {
    display: flex;
    align-items: center;
    gap: 0.7em;
  }
  .pc-meta .date {
    margin-left: auto;
    color: var(--text-secondary);
    font-size: 0.95em;
    white-space: nowrap;
  }
}

/* --- States --- */
.loading-state,
.empty-state {
  padding: 3em 0;
  border-radius: var(--radius-medium);
}
.spinner {
  width: 30px;
  height: 30px;
  border-width: 3px;
}
.empty-state i {
  font-size: 2.5em;
}

/* New, more subtle hover for small/no-image cards */
.article-card-link:hover .article-card.small-image,
.article-card-link:hover .article-card.no-image {
  transform: translateY(-3px);
  box-shadow: var(--shadow-medium);
}
</style>
